package com.hue.gulimail.gulimailware.dao;

import com.hue.common.to.mq.WareOrderTaskDetailTo;
import com.hue.gulimail.gulimailware.entity.WareSkuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品库存
 *
 * @author yaofengqiao
 * @email 451737523@qq.com
 * @date 2021-08-12 16:54:57
 */
@Mapper
public interface WareSkuDao extends BaseMapper<WareSkuEntity> {
    @Select("select sum(stock-stock_locked) from wms_ware_sku where sku_id=#{skuId}")
    Integer hasStock(@Param("skuId") Long skuId);

    /**
     * 查询一个skuId在那些仓库还有库存，返回仓库的Id集合
     *
     * @param skuId
     * @return
     */
    @Select("select ware_id from wms_ware_sku where sku_id = #{skuId} and stock - stock_locked > 0")
    List<Long> wareIdHasSkuIdStock(Long skuId);

    /**
     * 根据wareId/skuId/count锁定库存,返回影响的行数
     *
     * @param wareId
     * @param skuId
     * @param count
     * @return
     */
    @Update("update wms_ware_sku set stock_locked = stock_locked + #{count}" +
            " where sku_id = #{skuId} and ware_id = #{wareId} and stock - stock_locked > #{count}")
    Long lockSkuId(@Param("wareId") Long wareId, @Param("skuId") Long skuId, @Param("count") Integer count);

    @Update("update wms_ware_sku set stock_locked = stock_locked - #{skuNum}" +
            " where sku_id = #{skuId} and ware_id = #{wareId}")
    void unlockStock(WareOrderTaskDetailTo wareOrderTaskDetail);
}
